﻿using Common;
using Model;
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;

namespace Bussiness
{
    public delegate void ONShowMessageHandler(DeviceMessage message);
    public delegate void ONUpdateDeviceStateHandler(Device device);
    public class UI
    {
        public event ONShowMessageHandler ONShowMessage;
        public event ONUpdateDeviceStateHandler ONUpdateDeviceState;
        public ConcurrentQueue<DeviceMessage> _DeviceMessageQueue = new ConcurrentQueue<DeviceMessage>();

        public void StartShowMessage()
        {
            Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    while (_DeviceMessageQueue.Count > 0)
                    {
                        DeviceMessage deviceMessage;
                        bool success = _DeviceMessageQueue.TryDequeue(out deviceMessage);
                        if (success)
                        {
                            LOG.Write(deviceMessage.Content);
                            ONShowMessage(deviceMessage);
                        }
                    }
                }
            });
        }

        public void UpdateDeviceState(Device device)
        {
            ONUpdateDeviceState(device);
        }
    }
}
